//
//  RegisterController.swift
//  BodyBuilding
//
//  Created by franze on 2017/10/6.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class RegisterController: UIViewController {
    let nameField = UITextField()
    let pwdField = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewConfiguration()
    }
    
    func viewConfiguration(){
        view.backgroundColor = UIColor.white
        
        struct Content{
            let y:[CGFloat] = [220,280]
            let name = ["手 机 号:","密       码:"]
        }
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: nil)
        view.addSubview(navBar)
        
        navBar.backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
        
        let titleLabel = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 40)))
        titleLabel.textColor = UIColor(red: 18/255, green: 150/255, blue: 219/255, alpha: 1)
        titleLabel.center = CGPoint(x: view.center.x, y: 120)
        titleLabel.font = UIFont.boldSystemFont(ofSize: titleLabel.bounds.height)
        titleLabel.textAlignment = .center
        titleLabel.text = "注册"
        view.addSubview(titleLabel)
        
        let btn = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH-100, height: 40)))
        btn.setTitle("确认", for: .normal)
        btn.backgroundColor = UIColor(red: 18/255, green: 150/255, blue: 219/255, alpha: 1)
        btn.layer.cornerRadius = 5
        btn.layer.masksToBounds = true
        btn.center = CGPoint(x: view.center.x, y: HEIGHT-250)
        view.addSubview(btn)
        
        let content = Content()
        var num = 0
        for textField in [nameField,pwdField]{
            textField.frame = CGRect(x: (WIDTH-275)/2+75, y: 0, width: 200, height: 25)
            textField.borderStyle = .roundedRect
            textField.center.y = content.y[num]
            view.addSubview(textField)
            num += 1
        }
        
        for index in 0..<content.y.count{
            let label = UILabel(frame: CGRect(x: (WIDTH-275)/2, y: 0, width: 72, height: 16))
            label.font = UIFont.systemFont(ofSize: 16)
            label.text = content.name[index]
            label.center.y = content.y[index]
            view.addSubview(label)
        }
        
        btn.addTarget(self, action: #selector(register), for: .touchUpInside)
    }
    
    @objc func register(){
        let parameter:NSDictionary = ["tel":nameField.text!,"password":pwdField.text!]
        Networking.shared.register(parameter: parameter) { 
            self.back()
        }
    }
    
    @objc func back(){
        _ = navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
